﻿using MapGeneration;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoleCustom.Config;
using RoleCustom.Main;
using UnityEngine;
using Utils;


namespace RoleCustom.Event
{
    /// <summary>
    /// Scientis Event
    /// </summary>
    public class ScientistEvent
    {
        [PluginEvent(ServerEventType.PlayerSpawn)]
        public void OnPlayerSpawn(PlayerSpawnEvent ev)
        {
            if (ev.Player.Role != PlayerRoles.RoleTypeId.Scientist) return;

            TimeCoroutine.Delay(0.5f, () =>
            {
                if (Round.IsLocked) return;

                //PlayerUtils.SetHP(ev.Player,
                //    CodeLoad.Singleton.Config.ScientistEvent.Health,
                //    CodeLoad.Singleton.Config.ScientistEvent.Health);


                ConfigScientistEvent config = CodeLoad.Singleton.Config.ScientistEvent;
                bool isBase = UnityEngine.Random.Range(0, 1f) <= config.PerBaseScientis;
                bool isCoor = UnityEngine.Random.Range(0, 1f) <= config.PerResearchCoordinator;
                bool isZone = UnityEngine.Random.Range(0, 1f) <= config.PerZoneManager;
                bool isMana = UnityEngine.Random.Range(0, 1f) <= config.PerFacilityManager;

                ev.Player.Health = CodeLoad.Singleton.Config.ScientistEvent.Health;
                ev.Player.ArtificialHealth = CodeLoad.Singleton.Config.ScientistEvent.AHealth;

                if (isBase)
                {
                    this.SpawnAsScientist1(ev);
                    this.SpawnPoint(ev);
                    return;
                }

                if (isCoor)
                {
                    this.SpawnAsCoordinator(ev);
                    this.SpawnPoint(ev);
                    return;
                }

                if (isZone)
                {
                    this.SpawnAsZoneManager(ev);
                    this.SpawnPoint(ev);
                    return;
                }

                if (isMana)
                {
                    this.SpawnAsFacilityManager(ev);
                    this.SpawnPoint(ev);
                    return;
                }

                this.SpawnAsScientist2(ev);
                this.SpawnPoint(ev);
                return;
            });
        }

        [PluginEvent(ServerEventType.PlayerDeath)]
        public void OnPlayerDeath(PlayerDeathEvent ev)
        {
            if (ev.Player.Role != PlayerRoles.RoleTypeId.Scientist) return;

            ev.Player.ReferenceHub.serverRoles.SetColor("black");
            ev.Player.ReferenceHub.serverRoles.SetText(string.Empty);
        }



        private void SpawnAsScientist1(PlayerSpawnEvent ev)
        {
            // P1：获取描述
            string identity = BadageTxtUtils.CreateScientist(0);
            // P2：应用描述
            ev.Player.ReferenceHub.serverRoles.SetColor("yellow");
            ev.Player.ReferenceHub.serverRoles.SetText(identity);
            // P3：生成公告
            string tag = $"<color=yellow>{identity}</color>";
            MessageStringUtils.GernerateIdentity(tag, out string output);

            // P4：道具补充
            ev.Player.RemoveItems(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.ArmorLight);
            ev.Player.AddItem(ItemType.Flashlight);
            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);
        }
        private void SpawnAsScientist2(PlayerSpawnEvent ev)
        {
            string identity = BadageTxtUtils.CreateScientist(1);
            ev.Player.ReferenceHub.serverRoles.SetColor("yellow");
            ev.Player.ReferenceHub.serverRoles.SetText(identity);

            string tag = $"<color=yellow>{identity}</color>";
            MessageStringUtils.GernerateIdentity(tag, out string output);

            ev.Player.RemoveItems(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.ArmorLight);
            ev.Player.AddItem(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.Flashlight);
            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);
        }
        private void SpawnAsCoordinator(PlayerSpawnEvent ev)
        {
            string identity = BadageTxtUtils.CreateScientist(2);
            ev.Player.ReferenceHub.serverRoles.SetColor("yellow");
            ev.Player.ReferenceHub.serverRoles.SetText(identity);

            string tag = $"<color=yellow>{identity}</color>";
            MessageStringUtils.GernerateIdentity(tag, out string output);

            ev.Player.RemoveItems(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.ArmorLight);
            ev.Player.AddItem(ItemType.Flashlight);
            ev.Player.AddItem(ItemType.KeycardResearchCoordinator);
            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);
        }
        private void SpawnAsZoneManager(PlayerSpawnEvent ev)
        {
            string identity = BadageTxtUtils.CreateScientist(3);
            ev.Player.ReferenceHub.serverRoles.SetColor("yellow");
            ev.Player.ReferenceHub.serverRoles.SetText(identity);

            string tag = $"<color=yellow>{identity}</color>";
            MessageStringUtils.GernerateIdentity(tag, out string output);

            ev.Player.RemoveItems(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.ArmorLight);
            ev.Player.AddItem(ItemType.KeycardZoneManager);
            ev.Player.AddItem(ItemType.Flashlight);
            ev.Player.AddItem(ItemType.Radio);

            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);
        }
        private void SpawnAsFacilityManager(PlayerSpawnEvent ev)
        {
            string identity = BadageTxtUtils.CreateScientist(4);
            ev.Player.ReferenceHub.serverRoles.SetColor("yellow");
            ev.Player.ReferenceHub.serverRoles.SetText(identity);

            string tag = $"<color=yellow>{identity}</color>";
            MessageStringUtils.GernerateIdentity(tag, out string output);

            ev.Player.RemoveItems(ItemType.KeycardScientist);
            ev.Player.AddItem(ItemType.ArmorLight);
            ev.Player.AddItem(ItemType.KeycardFacilityManager);
            ev.Player.AddItem(ItemType.Flashlight);
            ev.Player.AddItem(ItemType.Radio);
            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);
        }


        private void SpawnPoint(PlayerSpawnEvent ev)
        {
            if (Round.IsLocked) return;

            int lucky = UnityEngine.Random.Range(0, 100);


            if (lucky > 90)
            {
                Log.Info($"科学家|{ev.Player.Nickname}|刷新至 LCZ330");
                bool isGet = SpawnPointUtils.TryGetSpawnPoint(RoomName.Lcz330, new Vector3(-5, 1, 0), out Vector3 point);
                if (!isGet) return;

                ev.Player.Position = point;
                return;
            }

            if (lucky > 80)
            {
                Log.Info($"科学家|{ev.Player.Nickname}|刷新至 LczComputerRoom");
                bool isGet = SpawnPointUtils.TryGetSpawnPoint(RoomName.LczComputerRoom, new Vector3(-1, 1, 2), out Vector3 point);
                if (!isGet) return;

                ev.Player.Position = point;
                return;
            }

            if (lucky > 70)
            {
                Log.Info($"科学家|{ev.Player.Nickname}|刷新至 LczComputerRoom");
                bool isGet = SpawnPointUtils.TryGetSpawnPoint(RoomName.LczComputerRoom, new Vector3(2.5f, 1, -2.5f), out Vector3 point);
                if (!isGet) return;

                ev.Player.Position = point;
                return;
            }
        }
    }
}